public class Content extends AtomBase {

	public String ctype { get; set; }
	public URI src { get; set; }
	public Text theContent { get; set; }
	public Blob theMediaContent { get; set; }
	public String mediaType { get; set; }
	public VirElement node { get; set; }
	
	public Content() {
		theContent = new Text();
	}
	
	public Content(XmlStreamReader reader) {
		System.debug(LoggingLevel.WARN, '\n\n\n\n Entering content parsing...\n\n\n\n');
		String tagName = 'content';
		String tagValue;
		handleAttributes(reader, tagName);
		while (reader.hasNext()) {
			reader.next();
			if (reader.getEventType() == XmlTag.END_ELEMENT) {
				System.debug(LoggingLevel.WARN, '\n\n End Element ...\n');
				if (tagName == 'content') {
					theContent = new Text(tagValue);
					theContent.textType = ctype;
					return;
				}
			} else if (reader.getEventType() == XmlTag.CHARACTERS) {
				System.debug(LoggingLevel.WARN, '\n\n Character Event...\n');
                if ( ! reader.isWhitespace() && reader.hasText()) {
                	if (tagValue == null) {
                		tagValue = reader.getText();
                	} else {
	                    tagValue += reader.getText();  // add text to current element
                	}
                	System.debug(LoggingLevel.WARN, '\n\n\t Tag value: ' + tagValue + '\n');
                }
            } else if (reader.getEventType() == XmlTag.START_ELEMENT) {
				System.debug(LoggingLevel.WARN, '\n\n Strart content element...\n');
				tagValue = '';
            	tagName = reader.getLocalName();
			}
		}
	}
	
	private void handleAttributes(XmlStreamReader reader, String tagName) {
		for (integer i=0; i<reader.getAttributeCount(); i++) {
       		String name = reader.getAttributeLocalName(i);
       		String value = reader.getAttributeValueAt(i);
       		System.debug(LoggingLevel.WARN, '\n\n\t In the attribute loop: ' + name + ' = ' + value + '\n');
    		if (name == 'type') {
    			ctype = value;
    		} else if (name == 'src') {
    			src = new URI(value);
    		} else if (name == 'mediatype') {
    			mediatype = value;
    		}
        }
       
	}
	
	public void setContentType(String ctype) {
		this.ctype = ctype;
		theContent.textType = ctype;
		theContent.tagName = 'content';
	}
	
	public override String toAPP() {
		String output = '<content ';
		if (ctype != null) output += 'type="' + ctype + '" ';
		if (src != null) output += 'src="' + src.url + '" ';
		if (mediatype != null) output += 'mediatype="' + mediatype + '" ';
		output += '>' + theContent;
		output += '</content>\n';
		return output;
	}
}